package cn.school.monitor.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.school.classManage.service.SchoolClassService;
import cn.school.classManage.vo.SchoolClassVO;
import cn.school.common.api.CommonPage;
import cn.school.common.utils.StringUtils;
import cn.school.monitor.entity.ClassAppsInstalledDo;
import cn.school.monitor.service.AppsInstalledService;
import cn.school.student.entity.StudentInfo;
import cn.school.student.service.StudentInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @className: AppsInstalledServiceImpl
 * @author: Alex
 * @date: 2023/3/30
 **/
@Service
public class AppsInstalledServiceImpl implements AppsInstalledService {
	@Resource
	private SchoolClassService schoolClassService;
	
	@Resource
	private StudentInfoService studentInfoService;
	
	@Override
	public CommonPage<ClassAppsInstalledDo> findClassAppsInstallList(SchoolClassVO vo) {
		// 查询班级信息
		IPage<SchoolClassVO> schoolClassPageList = schoolClassService.findSchoolClassPageList(vo);
		List<ClassAppsInstalledDo> classAppsInstalledDos = new ArrayList<>();
		
		IPage<ClassAppsInstalledDo> iPage = new Page<>();
		iPage.setSize(schoolClassPageList.getSize());
		iPage.setCurrent(schoolClassPageList.getCurrent());
		iPage.setTotal(schoolClassPageList.getTotal());
		iPage.setPages(schoolClassPageList.getPages());
		
		// 查询班级学生信息
		List<SchoolClassVO> records = schoolClassPageList.getRecords();
		List<Long> classIds = records.stream().map(SchoolClassVO::getId).collect(Collectors.toList());
		if (CollectionUtil.isEmpty(classIds)) {
			iPage.setRecords(classAppsInstalledDos);
			return CommonPage.restPage(iPage);
		}
		List<StudentInfo> studentInfos = studentInfoService.getStudentInfosByClassIds(classIds);
		Map<Long, List<StudentInfo>> listMap = studentInfos.stream().collect(Collectors.groupingBy(StudentInfo::getClassId));
		
		// 计算安装app人数 构建返回对象
		for (SchoolClassVO classVO : records) {
			ClassAppsInstalledDo installedDo = new ClassAppsInstalledDo();
			BeanUtils.copyProperties(classVO, installedDo);
			List<StudentInfo> infoList = listMap.get(installedDo.getId());
			if (CollectionUtil.isEmpty(infoList)) {
				installedDo.setStuInstall(0);
				installedDo.setStuUninstall(0);
				installedDo.setGuaInstall(0);
				installedDo.setGuaUninstall(0);
				classAppsInstalledDos.add(installedDo);
				continue;
			}
			// 构造安装了app与未安装app的学生对象
			buildInstalledData(classVO, installedDo, infoList);
			classAppsInstalledDos.add(installedDo);
		}
		
		iPage.setRecords(classAppsInstalledDos);
		return CommonPage.restPage(iPage);
	}
	
	private void buildInstalledData(SchoolClassVO classVO, ClassAppsInstalledDo installedDo, List<StudentInfo> infoList) {
		List<StudentInfo> stuUnInstall = infoList.stream().filter(studentInfo -> StringUtils.isEmpty(studentInfo.getStRegistrationId())).collect(Collectors.toList());
		installedDo.setStuInstall(classVO.getPersonCount() - stuUnInstall.size());
		installedDo.setStuUninstall(stuUnInstall.size());
		installedDo.setUnInstallStus(stuUnInstall);
		// 求差集
		List<StudentInfo> stuInstall = infoList.stream().filter(studentInfo -> {
			return !stuUnInstall.contains(studentInfo);
		}).collect(Collectors.toList());
		installedDo.setInstallStus(stuInstall);
		
		List<StudentInfo> guaUnInstall = infoList.stream().filter(studentInfo -> StringUtils.isEmpty(studentInfo.getGuRegistrationId())).collect(Collectors.toList());
		installedDo.setGuaInstall(classVO.getPersonCount() - guaUnInstall.size());
		installedDo.setGuaUninstall(guaUnInstall.size());
		installedDo.setUnInstallGuas(guaUnInstall);
		List<StudentInfo> guaInstalled = infoList.stream().filter(studentInfo -> {
			return !guaUnInstall.contains(studentInfo);
		}).collect(Collectors.toList());
		installedDo.setInstallGuas(guaInstalled);
	}
}
